/**
 * Created by Churcy on 2017/1/10.
 */
window.onload = function() {
	White.searchWhite();
}

var White = {

	searchWhite : function() {
		$("#searchBTN").on('click', function() {
			var conditions = getCurrentConditions();
			conditions.pageNo = 1;
			ajaxSearchWhiteList(conditions);
		});
		$("#resetBTN").on('click', function() {
			resetFn();
		});
	}
}
function searchFn() {
	var conditions = getCurrentConditions();
	console.log("searchFN")
	var response = ajaxSearchWhiteList(conditions);
}
function resetFn() {
	$("#userId").val("");
	$("#userName").val("");
	$('select[name="status"]').prop('selectedIndex', 0);

}

function getCurrentConditions() {
	console.log("getCurrentConditions");
	var userId = $("#userId").val();
	var userName = $("#userName").val();
	var status = $('select[name="status"]').val();
	var pageSize = $("#n").val();
	var pageNo = $("#p").val();
	var appKey = $("#APP_KEY").val();
	console.log("val取值:"+status);
	var conditions = {
		userId : encodeURI(userId),
		userName : encodeURI(userName),
		status : status ,
		pageIndex : pageNo,
		appKey : appKey
	};
	return conditions;

}
function ajaxSearchWhiteList(conditions) {
	console.log("ajaxSearchWhiteList");
	$.ajax({
		type : "GET",
		url : PROJECT_CTX + "/trafficback/whiteList",
		data : conditions,
		dataType : "json",
		success : function(data) {
			console.log(data)
			var result = eval(data);
			var resultCode = result.status.code;
			console.log(resultCode)
			console.log(result.line);
			if (1 == resultCode) {
				// 清空当前数据
				var whiteList = $("#whiteList").empty();
				// 填写查询出来的数据
				$.each(result.line, function(i, item) {
					var tr = "<tr>";
					
					tr += "<td >";
					tr += item.userId;
					tr += "</td>"

					tr += "<td >";
					tr += item.userName;
					tr += "</td>"

					tr += "<td >";
					if(item.status == 0){
						tr += "正常";
					}
					if(item.status == 1){
						tr += "禁用";
					}
					tr += "</td>"
								
					tr += "<td >";
					tr += item.createTime;
					tr += "</td>"
						
					tr += "<td >";
					tr += item.price;
					tr += "</td>"
						
					tr += "<td >"; 
					tr += " <a class='table-link primary' title='修改' href='"+PROJECT_CTX+"/traffic/white/info?wlId="+item.wlId+"&appKey="+conditions.appKey+"'>修改</a>";
					tr += "</td >";

					tr += "</tr>";
					whiteList.append(tr);
					// 更新分页栏
					reloadPage(result);
				});
			}

		}

		,
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			console.log(XMLHttpRequest.status);
			console.log(XMLHttpRequest.readyState);
			console.log(textStatus);
		}
	});
}

function formatDate(date) {
	var date = new Date(date);
	var month = date.getMonth() + 1;
	if (month < 10) {
		month = "0" + month;
	}
	var today = date.getDate();
	if (today < 10) {
		today = "0" + today;
	}
	return date.getFullYear() + "-" + month + "-" + today;
}
$("#normal").on('click',function(){
	$("#normal").removeClass().addClass("btn btn-primary");
	$("#unable").removeClass().addClass("btn btn-default");
	
});
$("#unable").on('click',function(){
	$("#normal").removeClass().addClass("btn btn-default");
	$("#unable").removeClass().addClass("btn btn-primary");
	
});